//查询接口

/**
 * 查询我占领的房间
 * @returns {*}
 */
module.exports.findMyRoom = () => {
    return Object.values(Game.rooms).filter(room => room.controller && room.controller.my);
}


/**
 * 查找旗帜
 * @param flagType
 * @param number
 * @returns {Flag[]|Flag}
 */
module.exports.findFlagList = (flagType, number = 0) => {
    let flags = Object.values(Game.flags);
    let color=flagType;
    if (number === 0) {
        return flags.filter(flag => flag.color === color);
    }
    return (flags.filter(flag => flag.color === color)).pop();
}

/**
 * 查找房间旗帜
 * @param room
 * @param flagType
 * @returns {Flag[]|Flag}
 */
module.exports.findRoomFlagList = (room,flagType) => {
    return room.find(FIND_FLAGS, {
        filter:
            flag => flag.color === flagType
    });
}

/**
 * 全部creep查找,可通过房间查找
 * @param role
 * @param room
 * @returns {Creep[]}
 */
module.exports.findRoleCreeps = (role, room = undefined) => {
    return Object.values(Game.creeps).filter(creep => {
        return creep.memory.role === role && (room === undefined || creep.room.name === room.name)
    })
}

/**
 * 寻找房间内存储容器
 * @param room
 * @returns {*}
 */
module.exports.findStore = room => {
    return room.find(FIND_STRUCTURES, {
        filter: build => {
            return build.structureType === STRUCTURE_SPAWN || build.structureType === STRUCTURE_EXTENSION || build.structureType === STRUCTURE_CONTAINER || build.structureType === STRUCTURE_STORAGE || build.structureType===STRUCTURE_TOWER
        }
    })
}

/**
 * 寻找房间内核心存储容器
 * @param room
 * @returns {*}
 */
module.exports.findBaseStore = room => {
    return room.find(FIND_STRUCTURES, {
        filter: build => {
            return build.structureType === STRUCTURE_SPAWN || build.structureType === STRUCTURE_EXTENSION|| build.structureType===STRUCTURE_TOWER
        }
    })
}

module.exports.findEnergyStore = room => {
    return room.find(FIND_STRUCTURES, {
        filter: build => {
            return build.structureType === STRUCTURE_CONTAINER || build.structureType === STRUCTURE_STORAGE
        }
    })
}

/**
 * 寻找建筑
 * @param room
 * @param structure
 * @returns {*}
 */
module.exports.findBuild = (room, structure) => {
    return room.find(FIND_STRUCTURES, {
        filter: build => build.structureType === structure
    })
}

/**
 * find close the list
 */
module.exports.findBestClose = (creep, list, minSize = 1) => {
    let rangeList;
    while (minSize < 16) {
        rangeList = creep.pos.findInRange(list, minSize);
        if (rangeList.length > 0) {
            return rangeList[0].id;
        }
        if (minSize <= 5) {
            minSize += 2;
        } else {
            minSize = 2 * minSize;
        }
    }
    return list[0].id;
}
//find link
module.exports.findLink=room=>{
   return  room.find(FIND_STRUCTURES,{filter:build=>build.structureType===STRUCTURE_LINK});
}